import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
 * 
 */
public class Atc extends DataCategory
{
    public Atc()
    {
    }
    public boolean initData(int id,int value)
    {
        if(id<=0x0F)
            switch(id)
            {
                default:
                    return false;
            }
        else
            switch(id)
            {
                case TOOL_MAGAZINE_CAPACITY:
                    toolMCapacity = Integer.valueOf(value);
                    return true;
                case MAXIMUM_TOOL_INDEX:
                    maxToolIndex = Integer.valueOf(value);
                    return true;
                case TOOL_SELECTION_TYPE:
                    toolSType = Integer.valueOf(value);
                    return true;
                case SELECTED_TOOL_INDEX:
                    selectedToolIndex = Integer.valueOf(value);
                    return true;
                case NEXT_TOOL_INDEX:
                    nextToolIndex = Integer.valueOf(value);
                    return true;
                default:
                    return false;
            }
    }
    public boolean initData(int id,long value)
    {
        return initData(id,(int) value);
    }
    public Component getView(Id viewID)
    {
        JPanel panel = new JPanel();
        JPanel p_aux1;
        switch(viewID)
        {
            case TOOL_MAGAZINE_CAPACITY:
                panel.setLayout(new GridLayout(1,2));
                p_aux1 = new JPanel(new GridBagLayout());
                JLabel toolMCapacityLabel = NewComp.getLabel("toolMCapacity","titleLabelTFrame","blue");
                JFormattedTextField toolMCapacityField = NewComp.getFTF( 2, "textFieldNumTFrame");
                toolMCapacityField.setValue(toolMCapacity);
                p_aux1.add(toolMCapacityField,new GridBagConstraints());
                panel.add(toolMCapacityLabel);
                panel.add(p_aux1);
                return panel;
            case MAXIMUM_TOOL_INDEX:
                panel.setLayout(new GridLayout(1,2));
                p_aux1 = new JPanel(new GridBagLayout());
                JLabel maxToolIndexLabel = NewComp.getLabel("maxToolIndex","titleLabelTFrame","blue");
                JFormattedTextField maxToolIndexField = NewComp.getFTF( 2, "textFieldNumTFrame");
                maxToolIndexField.setValue(maxToolIndex);
                p_aux1.add(
                    maxToolIndexField,
                    new GridBagConstraints());
                panel.add(maxToolIndexLabel);
                panel.add(p_aux1);
                return panel;
            case TOOL_SELECTION_TYPE:
                panel.setLayout(new GridLayout(1,2));
                JPanel p_aux = new JPanel(new GridBagLayout());
                JLabel toolSTypeLabel = NewComp.getLabel("toolSType","titleLabelTFrame","blue");
                JTextField toolSTypeField = 
                    NewComp.getTxtField(
                        toolSTypeOpt[toolSType], 
                        "textFieldTFrame"
                    );
                p_aux.add(toolSTypeField, new GridBagConstraints());
                panel.add(toolSTypeLabel);
                panel.add(p_aux);
                return panel;
            case SELECTED_TOOL_INDEX:
                //selectedToolIndex = PathPlanning.getActiveTOIndex();
                panel.setLayout(new GridLayout(1,2));
                p_aux1 = new JPanel(new GridBagLayout());
                JLabel selectedToolIndexLabel = NewComp.getLabel("selectedToolIndex","titleLabelTFrame","blue");
                selectedToolIndexField = NewComp.getFTF( 2, "textFieldNumTFrame");
                selectedToolIndexField.setValue(selectedToolIndex);
                p_aux1.add(selectedToolIndexField,new GridBagConstraints());
                panel.add(selectedToolIndexLabel);
                panel.add(p_aux1);
                return panel;
            case NEXT_TOOL_INDEX:
                panel.setLayout(new GridLayout(1,2));
                p_aux1 = new JPanel(new GridBagLayout());
                JLabel nextToolIndexLabel = NewComp.getLabel("nextToolIndex","titleLabelTFrame","blue");
                nextToolIndexField = NewComp.getFTF( 2, "textFieldNumTFrame");
                nextToolIndexField.setValue(nextToolIndex);
                p_aux1.add(nextToolIndexField,new GridBagConstraints());
                panel.add(nextToolIndexLabel);
                panel.add(p_aux1);
                return panel;
            default:
                return null;
        }
    }
    public Component getEditor(Id editorID)
    {
        JPanel panel;
        JPanel p_aux1;
        switch(editorID)
        {
            case TOOL_MAGAZINE_CAPACITY:
                panel = new JPanel(new GridLayout(1,2));
                p_aux1 = new JPanel(new GridBagLayout());
                JLabel toolMCapacityLabel = NewComp.getLabel("toolMCapacity","titleLabelBFrame","black");
                toolMCapacityField = NewComp.getIntegerTxtField("textFieldNumBFrame");
                toolMCapacityField.setValue(toolMCapacity);
                toolMCapacityField.getDocument().addDocumentListener(
                    new DocumentListener() 
                    {
                        public void insertUpdate(DocumentEvent event) 
                        {
                            if(changed.lastIndexOf(Id.TOOL_MAGAZINE_CAPACITY) == -1)
                                changed.add(Id.TOOL_MAGAZINE_CAPACITY);
                        }
                        public void removeUpdate(DocumentEvent event) 
                        {
                            if(changed.lastIndexOf(Id.TOOL_MAGAZINE_CAPACITY) == -1)
                                changed.add(Id.TOOL_MAGAZINE_CAPACITY);
                        }
                        public void changedUpdate(DocumentEvent event){}
                    }
                );
                p_aux1.add(toolMCapacityField,new GridBagConstraints());
                panel.add(toolMCapacityLabel);
                panel.add(p_aux1);
                return panel;
            case MAXIMUM_TOOL_INDEX:
                panel = new JPanel(new GridLayout(1,2));
                p_aux1 = new JPanel(new GridBagLayout());
                JLabel maxToolIndexLabel = NewComp.getLabel("maxToolIndex","titleLabelBFrame","black");
                maxToolIndexField = NewComp.getIntegerTxtField("textFieldNumBFrame");
                maxToolIndexField.setValue(maxToolIndex);
                maxToolIndexField.getDocument().addDocumentListener(
                    new DocumentListener() 
                    {
                        public void insertUpdate(DocumentEvent event) 
                        {
                            if(changed.lastIndexOf(Id.MAXIMUM_TOOL_INDEX) == -1)
                                changed.add(Id.MAXIMUM_TOOL_INDEX);
                        }
                        public void removeUpdate(DocumentEvent event) 
                        {
                            if(changed.lastIndexOf(Id.MAXIMUM_TOOL_INDEX) == -1)
                                changed.add(Id.MAXIMUM_TOOL_INDEX);
                        }
                        public void changedUpdate(DocumentEvent event){}
                    }
                );
                p_aux1.add(maxToolIndexField,new GridBagConstraints());
                panel.add(maxToolIndexLabel);
                panel.add(p_aux1);
                return panel;
            case TOOL_SELECTION_TYPE:
                panel = NewComp.getPanel("toolSType","titleBorderBFrame",new GridLayout(1,2));
                toolSType_Aux = toolSType;
                fixedButton =NewComp.getRadioButton( 
                    "fixed", 
                    "titleRButton",
                    "blue",
                    toolSTypeOpt[toolSType].equals("fixed"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            toolSType_Aux = Integer.valueOf(0);
                            if(changed.lastIndexOf(Id.TOOL_SELECTION_TYPE) == -1)
                                changed.add(Id.TOOL_SELECTION_TYPE);
                        }
                    }
                );
                randomButton =NewComp.getRadioButton( 
                    "random", 
                    "titleRButton",
                    "blue",
                    toolSTypeOpt[toolSType].equals("random"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            toolSType_Aux = Integer.valueOf(1);
                            if(changed.lastIndexOf(Id.TOOL_SELECTION_TYPE) == -1)
                                changed.add(Id.TOOL_SELECTION_TYPE);
                        }
                    }
                );
                ButtonGroup toolGroup = new ButtonGroup();
                toolGroup.add(fixedButton);
                toolGroup.add(randomButton);
                panel.add(fixedButton);
                panel.add(randomButton);
                return panel;
            case SELECTED_TOOL_INDEX:
                //selectedToolIndex = PathPlanning.getActiveTOIndex();
                panel = new JPanel(new GridLayout(1,2));
                p_aux1 = new JPanel(new GridBagLayout());
                JLabel selectedToolIndexLabel = NewComp.getLabel("selectedToolIndex","titleLabelBFrame","black");
                selectedToolIndexField = NewComp.getIntegerTxtField("textFieldNumBFrame");
                selectedToolIndexField.setValue(selectedToolIndex);
                selectedToolIndexField.getDocument().addDocumentListener(
                    new DocumentListener() 
                    {
                        public void insertUpdate(DocumentEvent event) 
                        {
                            if(changed.lastIndexOf(Id.SELECTED_TOOL_INDEX) == -1)
                                changed.add(Id.SELECTED_TOOL_INDEX);
                        }
                        public void removeUpdate(DocumentEvent event) 
                        {
                            if(changed.lastIndexOf(Id.SELECTED_TOOL_INDEX) == -1)
                                changed.add(Id.SELECTED_TOOL_INDEX);
                        }
                        public void changedUpdate(DocumentEvent event){}
                    }
                );
                p_aux1.add(selectedToolIndexField,new GridBagConstraints());
                panel.add(selectedToolIndexLabel);
                panel.add(p_aux1);
                return panel;
            default:
                return null;
        }
    }
    public String getDataCategoryID()
    {
        return "atc";
    }
    public String getValue(Id dataID)
    {
        switch(dataID)
        {
            case MAXIMUM_TOOL_INDEX:
                maxToolIndex.toString();
            default:
                return null;
        }
    }
    public boolean setCommandValue(int id,int value)
    {
        return initData( id, value);
    }
    public boolean setCommandValue(int id,long value)
    {
        return initData( id, value);
    }
    public boolean setDataValue(int id,int value)
    {
        if(!initData( id, value))
            return false;
        switch(id)
        {
            case SELECTED_TOOL_INDEX:
                selectedToolIndexField.setValue(Integer.valueOf(value));
                break;
            case NEXT_TOOL_INDEX:
                nextToolIndexField.setValue(Integer.valueOf(value));
                break;
        }
        return true;
    }
    public boolean setDataValue(int id,long value)
    {
        return setDataValue(id,(int) value);
    }
    public void applyChange()
    {
        int aux;
        while( changed.isEmpty() == false)
        {
            switch(changed.get(0))
            {
                case TOOL_MAGAZINE_CAPACITY:
                    aux = ((Number)toolMCapacityField.getValue()).intValue();
                    if((aux < 0)||(aux > (int)MAX_UINT_8))
                    {
                        toolMCapacityField.setValue(toolMCapacity);
                        toolMCapacityField.requestFocus();
                        return;
                    }else
                    {
                        if(aux == 0)
                            selectedToolIndexField.setEnabled(false);
                        else
                            selectedToolIndexField.setEnabled(true);
                        if(aux < maxToolIndex)
                            maxToolIndexField.setValue(aux);
                        toolMCapacity = aux;
                        InfoManager.addDataValue(
                            TOOL_MAGAZINE_CAPACITY,
                            toolMCapacity
                        );
                    }
                    break;
                case MAXIMUM_TOOL_INDEX:
                    aux = ((Number)maxToolIndexField.getValue()).intValue();
                    if((aux < 0)||(aux > toolMCapacity))
                    {
                        maxToolIndexField.setValue(maxToolIndex);
                        maxToolIndexField.requestFocus();
                        return;
                    }else
                    {
                        if(aux < selectedToolIndex)
                            selectedToolIndexField.setValue(aux);
                        maxToolIndex = aux;
                        InfoManager.addDataValue(
                            MAXIMUM_TOOL_INDEX,
                            maxToolIndex
                        );
                    }
                    break;
                case TOOL_SELECTION_TYPE:
                    toolSType = toolSType_Aux;
                    InfoManager.addDataValue(
                        TOOL_SELECTION_TYPE,
                        toolSType
                    );
                    break;
                case SELECTED_TOOL_INDEX:
                    aux = Integer.valueOf(
                        ((Number)selectedToolIndexField.getValue()).intValue());
                    if((aux < 0)||(aux > maxToolIndex))
                    {
                        selectedToolIndexField.setValue(selectedToolIndex);
                        selectedToolIndexField.requestFocus();
                        return;
                    }else
                    {
                        selectedToolIndex = aux;
                        InfoManager.addDataValue(
                            SELECTED_TOOL_INDEX,
                            selectedToolIndex
                        );
                    }
                    break;
                case OTHER:
                    break;
                default:
                    break;
            }
            changed.remove(0);
        }
    }
    public void cancelChange()
    {
        while( changed.isEmpty() == false)
        {
            switch(changed.get(0))
            {
                case TOOL_MAGAZINE_CAPACITY:
                    toolMCapacityField.setValue(toolMCapacity);
                    break;
                case MAXIMUM_TOOL_INDEX:
                    maxToolIndexField.setValue(maxToolIndex);
                    break;
                case TOOL_SELECTION_TYPE:
                    toolSType_Aux = toolSType;
                    if(toolSTypeOpt[toolSType].equals("fixed"))
                        fixedButton.setSelected(true);
                    else
                        randomButton.setSelected(true);
                    break;
                case SELECTED_TOOL_INDEX:
                    selectedToolIndexField.setValue(selectedToolIndex);
                case OTHER:
                    break;
                default:
                    break;
            }
            changed.remove(0);
        }
        changed.clear();
    }
    /**
     * Proporciona el índice máximo de herramienta presente
     * en la máquina
     * @return un entero igual al índice máximo de herramienta
     */
    public static int getMaxToolIndex()
    {
        return maxToolIndex;
    }
    public static int getSelectedToolIndex()
    {
        return selectedToolIndex;
    }
    static
    {
        toolMCapacity = new Integer(0);
        toolSType = new Integer(0);
        toolSType_Aux = new Integer(0);
        selectedToolIndex = new Integer(0);
        nextToolIndex = new Integer(0);
        maxToolIndex = new Integer(0);
    }
    private static final String[] toolSTypeOpt = {
        "fixed",
        "random"
    };
    private static Integer toolMCapacity;
    private static JFormattedTextField toolMCapacityField;
    private static Integer toolSType;
    private static Integer toolSType_Aux;
    private static JRadioButton fixedButton;
    private static JRadioButton randomButton;
    private static Integer selectedToolIndex;
    private static JFormattedTextField selectedToolIndexField;
    private static Integer nextToolIndex;
    private static JFormattedTextField nextToolIndexField;
    private static Integer maxToolIndex;
    private static JFormattedTextField maxToolIndexField;
}
